//
//  LYRequestManager.m
//  artist
//
//  Created by laotang on 16/4/4.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYRequestManager.h"
#import "LYUrlManager.h"
#import "LYPublicMethod.h"
#import "LYRequestInfo.h"
#import "LYUrlManager.h"


@implementation LYRequestManager
+ (instancetype)requestManager{
    
    return [[self alloc] initWithBaseURL:[NSURL URLWithString:[LYUrlManager serverUrl]] sessionConfiguration:nil];
}



- (NSURLSessionDataTask *)getDataWithRelativeUrl:(NSString *)relativeUrl Parameters:(id)parameters success:(void (^)(id _Nonnull responseObject))success failure:(void (^)(NSURLSessionDataTask * _Nullable dataTask, NSError * _Nonnull error))failure{
    
    return [self GET:relativeUrl parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        LYRequestInfo *info = [LYRequestInfo mj_objectWithKeyValues:responseObject];
        
        if(success){
            
            success(info.data);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}


- (NSURLSessionDataTask *)postWithRelativeUrl:(NSString *)relativeUrl Parameters:(id)parameters success:(void (^)(LYRequestInfo *requestInfo))success failure:(void (^)(NSURLSessionDataTask * _Nullable dataTask, NSError * _Nonnull error))failure{
    
    return [self POST:relativeUrl parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        LYRequestInfo *info = [LYRequestInfo mj_objectWithKeyValues:responseObject];
        
        if(success){
            success(info);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [MBProgressHUD showOnlyText:@"网络繁忙"];
        
    }];

    
}

-(NSURLSessionDataTask * _Nonnull)uploadDataWithRelativeUrl:(NSString *_Nullable)relativeUrl Parameters:(_Nullable id)parameters constructingBodyWithBlock:(void (^_Nullable)( id<AFMultipartFormData>  _Nonnull formData))formDataHandle success:(void (^_Nullable)(LYRequestInfo *_Nullable requestInfo))success failure:(void (^_Nullable)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failuer{
    
    return [self POST:relativeUrl parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        if(formDataHandle){
            formDataHandle(formData);
        }
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        if(success){
            
            LYRequestInfo *info = [LYRequestInfo mj_objectWithKeyValues:responseObject];
            success(info);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];
}

@end
